package hotel.quartz.oversea.job.task;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.constant.quartz.UrlMtEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Slf4j
@Component
public class MtSaveLocalGoodsInfoTask {
    private String timestamp = "mslgit"+String.valueOf(System.currentTimeMillis());
    @Autowired
    private RedisService redisService;
    @Autowired
    private RestTemplate restTemplate;
    private void setTimestamp(String timestamp){
        this.timestamp = timestamp;
    }

    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void executeSaveBasic() {
        log.info("Mt Save Local Goods Info Task {} >>>>>>>> 保存更新美团静态产品信息到本地库定时器 执行时间: {}", timestamp, DateUtil.getCurrTime());
        //从缓存中获取上架的酒店
        Long queue = redisService.queueSize(MeituanConstants.REDIS_QUEUE_SAVE_BASIC_HOTEL_GOODS_INFO);
        if(queue>0){
            SendUtils.dingMsgNotLog("[执行] pop产品静态信息队列保存到数据库 ", DateUtil.getCurrTime()+" 预计耗时："+queue/2/60+" 分", MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            Long newSize = queue;
            for (int i = 0; i < queue; i++) {
                if(null != redisService.get("QueuePopBasicSaveMtGoodsInfoJob_saveLocal_close")){
                    log.info("主动停止静态产品信息保存定时器");
                    break;
                }
                Result rs = request();
                log.info("Mt Save Local Goods Info Task  返回={}，i={}",rs,i);
                log.info("队列数量：{}",redisService.queueSize(MeituanConstants.REDIS_QUEUE_SAVE_BASIC_HOTEL_GOODS_INFO));
                if(redisService.queueSize(MeituanConstants.REDIS_QUEUE_SAVE_BASIC_HOTEL_GOODS_INFO)==0){
                    log.info("Mt Save Local Goods Info Task 队列缓存的待保存产品静态信息为0 break");
                    SendUtils.dingMsgNotLog("[结束] pop产品静态信息队列保存到数据库 ", DateUtil.getCurrTime()+" 成功更新保存,队列数量："+newSize, MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
                    break;
                }
            }
        }
        log.info("Mt Save Local Goods Info Task {} >>>>>>>> 保存更新美团静态产品信息到本地库定时器 结束时间: {},{}",timestamp, DateUtil.getCurrTime());

    }
    /**
     * 调用美团接口    主动查询上送的酒店是否存在变动数据，有则接口中操作存入缓存队列
     *
     * @return
     */
    private Result request() {
        String host = CommonUrlConstants.MEITUAN_HOST;
        String url = host+ UrlMtEnums.findUpdateGoods.UPDATE_SAVE_BASIC_INFO.getValue();
        Result response = JobRestTemplateUtils.post(url,null,restTemplate);
        if(response.getCode()!= CommonConstants.SUCCESS){
            log.error("静态产品信息保存更新定时器 调用美团系统接口失败："+ GsonUtils.GsonString(response));
        }
        return response;
    }



}
